﻿package com.tvein.sanguo.swc.action.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import fl.controls.TextInput;
	import fl.controls.TextArea;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolMailNew extends MovieClip
	{
		public static const NAME:String = "Tool-MailNew";

		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var txtTitle:TextInput;
		private var txtReceiver:TextInput;
		private var contentArea:TextArea;
		
		public function ToolMailNew()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtReceiver = new TextInput();
			txtReceiver.x = 90;
			txtReceiver.y = 65;
			txtReceiver.width = 120;
			txtReceiver.height = 25;
			this.addChild(txtReceiver);
			
			txtTitle = new TextInput();
			txtTitle.x = 310;
			txtTitle.y = 65;
			txtTitle.width = 280;
			txtTitle.height = 25;
			this.addChild(txtTitle);
			
			var style1:TextFormat = new TextFormat();
			style1.color = 0xff6600;
			style1.size = 16;
			style1.font = "隶书";
			style1.align = "center";
			
			var txtRole:TextField = txtReceiver.getChildAt(0) as TextField;
			txtReceiver.setStyle("textFormat", style1);
			txtRole.background = true;
			txtRole.backgroundColor = 0xCCCCCC;
			
			var txtInput:TextField = txtTitle.getChildAt(0) as TextField;
			txtTitle.setStyle("textFormat", style1);
			txtInput.background = true;
			txtInput.backgroundColor = 0xCCCCCC;
			
			contentArea = new TextArea();
			contentArea.x = 10;
			contentArea.y = 100;
			contentArea.width = 580;
			contentArea.height = 260;
			this.addChild(contentArea);
			
			var style2:TextFormat = new TextFormat();
			style2.color = 0xff6600;
			style2.size = 16;
			style2.font = "隶书";
			
			var txtArea:TextField = contentArea.getChildAt(0) as TextField;
			contentArea.setStyle("textFormat", style2);
			txtArea.background = true;
			txtArea.backgroundColor = 0x666666;
			
			btnSend.visible = false;

			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			btnSend.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);					
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			txtReceiver.text = params[1];
		}
		
		private function _keyDownHandler(evt:KeyboardEvent):void
		{									
			btnSend.visible = (txtReceiver.text != "") && (txtTitle.text != "") && (contentArea.text != "");
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
			btnSend.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["mail"]]));
					break;
				case "btnSend":
					dispatchEvent(new SwcEvent(NAME, ["-sendMail", [txtReceiver.text, txtTitle.text, contentArea.text]]));
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolMailNew"]));
					break;
			}
		}
	}
}